'use strict';
const db = uniCloud.database()
const postsCollection = db.collection('posts')

exports.main = async (event, context) => {
	const { title, content, images, userId, userName, userAvatar } = event
	
	// 参数校验
	if (!title || !content || !userId) {
		return {
			code: -1,
			msg: '参数不完整'
		}
	}
	
	try {
		// 创建帖子
		const result = await postsCollection.add({
			title,
			content,
			images: images || [],
			userId,
			userName,
			userAvatar,
			likeCount: 0,
			commentCount: 0,
			createTime: Date.now()
		})
		
		return {
			code: 0,
			msg: '发布成功',
			data: result
		}
	} catch (e) {
		return {
			code: -1,
			msg: '发布失败',
			error: e
		}
	}
}; 